window.workspace = function(){
	var response  = <?#$respons?>;
	var store, grid, win, form, sm;
	var sid = {'use':Ext.id(), 'expire':Ext.id()};
	function addForm(rows){
		if(!win) {
		    form = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 150,
		        fileUpload  : true,
		        labelAlign  : 'right',
		    	defaults	: {
		    		xtype	  : 'textfield'
		    	},
		    	items		: [{
					xtype		: 'hidden',
					name		: 'id',
					value		: 0
				},{
					xtype		: 'textfield',
					fieldLabel	: '标题[Title]',
					width		: 300,
					name		: 'title',
					maxLength   : 255
				},{
					xtype		: 'textarea',
					fieldLabel	: '跳转地址[Jump URL]',
					width		: 300,
					height      : 60,
					name		: 'link',
					emptyText   : '[http url jump url | profile:userid app inner jump to user profile | live:studioid app inner jump liveroom].',
					maxLength   : 255,
					allowBlank	: false
				},{
					xtype		: 'textfield',
					inputType 	: 'file', 
			 		fieldLabel	: '图片[Image]',
					name		: 'Filedata'
				},{
		    		xtype		: 'combo',
		            store		: new Ext.data.ArrayStore({
			        	fields: ['id','name'],
			        	data: main.cv2Array(response.use)
			    	}),
					displayField	: 'name',
					valueField		: 'id',
					mode			: 'local',
					emptyText		: 'plz selected',
		            fieldLabel		: '展示位置[ShowPosition]',
		            width			: 120,
		            allowBlank		: false,
					triggerAction	: 'all',
					hiddenName		: 'use',
		            name			: 'use'
		        },{
					xtype		: 'numberfield',
					fieldLabel	: '排序[Order]',
					width		: 120,
					name		: 'rank',
					value       : 0,
					allowBlank	: false
				},{
					xtype		: 'datetimefield',
					fieldLabel	: '过期时间[Expire]',
					width		: 160,
					name		: 'expire',
					allowBlank	: false
				},{
					xtype		: 'datetimefield',
					fieldLabel	: '开始时间[Start]',
					width		: 160,
					name		: 'start'
				}]
			});
			win = new Ext.Window({
				title	: '添加[Add]',
				iconCls	: 'icon-add',	
				width	: 520,
				items	: [form],
				closeAction : 'hide',
				url		: '/admin/live/activity/slidepic/dosave',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text:'Save', 
						iconCls:'icon-save',
						handler:function(){
						if(!form.getForm().isValid())
		        			return;
		        		form.getForm().submit({
			        		waitMsg 	: 'data submit processing...',
							waitTitle 	: 'Tips',
			        		clientValidation: true,
			        		url		: '/admin/live/activity/slidepic/dosave',
				        	success	: function(form, action) {
				        		store.reload();
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
		}
		var id = Ext.isObject(rows)? rows.id:0;
		win.setTitle((id > 0)? '编辑[Edit]':'添加[Add]');
		win.setIconClass((id > 0)? 'icon-edit':'icon-add');
		win.show();
		form.getForm().findField('Filedata').setRawValue('');
		if(id < 1) {
			form.getForm().reset();
		} else {
			form.getForm().loadRecord(rows);
		}
	};
	store = new Ext.data.JsonStore({
	    autoDestroy	: true,
	    url			: '/admin/live/activity/slidepic/dolist',
	    remoteSort	: true,
	    sortInfo	: {
            field		: 'rank',
            direction	: 'ASC'
		},
		baseParams	: {
			start	: 0,
			limit	: main.pageSize
		},
		autoLoad		: true,
	    root			: 'dataset',
	 	totalProperty	: 'total',
	    fields			: ['id', 'title', 'lng', 'src', 'studio', 'studioid',
	         'link', 'expire', 'start', 'rank', 'use', 'apns', 'stime']
	});
	sm   = new Ext.grid.CheckboxSelectionModel();
 	grid = {
		xtype		: 'grid',
		title		: response.title,
	    store		: store,
		region		: 'center', 
	    columns		: [
			sm,
			{header: '编号[ID]', width:60, sortable:true, dataIndex:'id'},
			{header: '标题[Title]', width:120, dataIndex:'title', renderer:function(v,meta,obj){
				return '<a href="'+obj.get('link')+'">'+v+'</a>';
			}},
			{header: '图片[Image]', width:80, dataIndex:'src', renderer:function(v){
				return '<img src="'+Ext.STATIC_URL+v+'" width="120" height="40" />';
			}},
			{header: '跳转地址[JumpUrl]', width:80, dataIndex:'link'},
			/*{header: 'RelateRoon', width:80, dataIndex:'studioid', renderer:function(v,meta,obj){
				return (v > 1)? obj.get('studio')+'['+v+']':'';
			}},*/
			{header: '展示位置[ShowPosition]', width:80, sortable:true, dataIndex:'use', renderer:function(v){
				return response.use[v];
			}},
			{header: '开始时间[Start]', width:100, dataIndex:'start'},
			{header: '过期时间[Expire]', width:100, dataIndex:'expire'},
			{header: '排序[Order]', width:80, sortable:true, dataIndex:'rank'},
			{header: '操作时间[OperatorTime]',width:120,dataIndex:'stime'}
		],
	    viewConfig	: {
	        forceFit	 : true,
	        scrollOffset : 0
	    },
		listeners	: {
			'destroy':function(){
    			win && win.close();
    		},'rowdblclick':function(grid, index, e){
		    	var data = grid.store.getAt(index).copy();
		    	addForm(data);
		    },
			'render':function(obj){
				var store = obj.store? obj.store:obj.getStore();  
			    var view  = obj.getView();
			    obj.tip  = new Ext.ToolTip({   
			        target: view.mainBody, 
			        delegate: '.x-grid3-row',
			        trackMouse: true,
			        renderTo: document.body, 
			        listeners: {
			            beforeshow: function updateTipBody(tip) {   
			                var rowIndex = view.findRowIndex(tip.triggerElement);  
			                var record   = store.getAt(rowIndex);
			                var html     = record.get('src');
			                if(/\.(png|jpg|jpeg|gif|bmp)$/i.test(html)) {
			                	html = '<img src="'+Ext.STATIC_URL+html+'" />';
			                }
			                tip.body.dom.innerHTML = html;   
			            }   
			        }   
			    });  
	    	}
		},	
	    sm			: sm,
	    stripeRows	: true,
	    tbar		: [{
			text	: 'REfresh',
			iconCls	: 'icon-refresh',
			url		: '/admin/live/activity/slidepic/dosave',
			handler : function(){
				store.reload(); 
			}
	    },'-',{
			text	: '添加[Add]',
			iconCls	: 'icon-add',
			handler : function() {
				addForm(0);
			}
		},'-',{
			text	: '删除[Del]',
			iconCls	: 'icon-delete',
			url		: '/admin/live/activity/slidepic/dodelete',
			handler : function(){
				var rcs = grid.sm.getSelections();
				if(Ext.isEmpty(rcs)) {
					Ext.Msg.alert('Tips', 'plz select delete records.');
					return;
				}
				var ids= [];
				Ext.each(rcs, function(item){
					ids.push(item.get('id'));
				});
				Ext.Msg.confirm('Tips','Do you sure to delete selected record, if delete can\'t be recover.',function(btn){
					if(btn=="yes") {
						Ext.Ajax.request({
							url		: '/admin/live/activity/slidepic/dodelete',
							params	: {'ids':ids.join(',')},
							success : function(resp, req){
								var data = Ext.decode(resp.responseText);
								if(data.code == 0) {
									store.reload(); 
								} else {
									Ext.Msg.alert('Tips', data.msg);
								}
							}
						});
					}
				});
			}
		},'-','<b>Search For：</b>','Status:',{
			xtype		: 'combo',
			store		: new Ext.data.ArrayStore({
		        fields: ['id','name'],
		        data: main.cv2Array(response.use)
		    }),
			displayField	: 'name',
			valueField		: 'id',
			mode			: 'local',
			triggerAction	: 'all',
			emptyText		: 'selected...',
			width			: 120,
			id  			: sid.use
		},'-','Expire:',{
			xtype		: 'combo',
			store		: new Ext.data.ArrayStore({
		        fields: ['id','name'],
		        data: main.cv2Array(response.expire)
		    }),
			displayField	: 'name',
			valueField		: 'id',
			mode			: 'local',
			triggerAction	: 'all',
			emptyText		: 'selected...',
			width			: 120,
			id  			: sid.expire
		},'-',{
			text			: 'Do Search',
			iconCls			: 'icon-search',
			handler: function() {
				store.baseParams.use    = Ext.getCmp(sid.use).getValue();
				store.baseParams.expire = Ext.getCmp(sid.expire).getValue();
				store.reload(); 
			}
		}],
		bbar		: new Ext.PagingToolbar({
			pageSize	: main.pageSize,
			store		: store,
			displayInfo	: true
		})
	};
	return grid;
}();